--[[

	GModRP - Real Garry's Mod Roleplay.
			By Slob187.
		

]]


MyInventory = {}
local SetJobList
local PlayerCommandList

function GMRP.AddJobCat( name, type )


	local Cat = vgui.Create( "DCollapsibleCategory" )
	Cat:SetTall( 50 )
	Cat:SetExpanded( 1 ) 
	Cat:SetLabel( name )


 	local CatList = vgui.Create( "DPanelList" )
	CatList:SetSpacing( 1 )
	CatList:SetPadding( 1 )
	CatList:SetAutoSize( true ) 
	CatList:EnableVerticalScrollbar( true ) 
	CatList:EnableHorizontal( false )
	Cat:SetContents( CatList )

	for k, v in pairs( Jobs ) do
	
		if ( Jobs[k][2] == type ) then
	
			local JobPanel = vgui.Create("DPanel")
			JobPanel:SetTall(75)
			JobPanel.Paint = function()
			
				surface.SetDrawColor( 180, 180, 180, 255 ) 
				surface.DrawRect( 0, 0, JobPanel:GetWide(), JobPanel:GetTall() )
				
			end

			local Title = vgui.Create("DLabel", JobPanel)
			Title:SetPos(90, 10)
			Title:SetText(k)
			Title:SetFont("GModRPFontOne")
			Title:SetColor( Color( 90, 111, 142, 225 ) )
			Title:SizeToContents() 
			Title:SetContentAlignment( 5 )				
			
			local jInfo = vgui.Create( "DLabel", JobPanel )	
			jInfo:SetPos(90, 40)
			jInfo:SetText( "Max: "..team.NumPlayers( Jobs[k][1] ).."/"..Jobs[k][3].."   Payday: "..Jobs[k][4] )
			jInfo:SetFont( "GModRPFontOne" )
			jInfo:SetColor( Color( 90, 111, 142, 225 ) )
			jInfo:SizeToContents() 
			jInfo:SetContentAlignment( 5 )				
			

			JobPanel.Choose = vgui.Create( "DButton", JobPanel ) 
			JobPanel.Choose:SetSize( 60, 20 )
			JobPanel.Choose:SetPos( 400, JobPanel:GetTall()/2-10 ) 
			JobPanel.Choose:SetText( "Choose" )
			JobPanel.Choose.DoClick = function()
			
				RunConsoleCommand( "gm_rp_job_change", Jobs[k][1] )	
		
			end	
			
			CatList:AddItem( JobPanel )	

		end

	end			
	
	SetJobList:AddItem( Cat )
	
end	


local function AddPlayerCommandButton( name, command )
	
	local PCButton = vgui.Create("DButton", RadioFrame)
	PCButton:SetTall( 20 )
	PCButton:SetText( name )
	PCButton.DoClick = function()
	
		LocalPlayer():ConCommand( command )
		
	end
	PlayerCommandList:AddItem( PCButton )
	
end


function GMRP.PlayerPanel()

	local PlayerPanel = vgui.Create("GMRPFrame")
	PlayerPanel:SetSize(700, 700) 
	PlayerPanel:SetTitle( "Player" ) 
	PlayerPanel:SetPos(ScrW() / 2 - PlayerPanel:GetWide() / 2, ScrH() / 2 - PlayerPanel:GetTall() / 2) --:O
	PlayerPanel:SetVisible( true )
	PlayerPanel:SetDraggable( true )
	PlayerPanel:SetBackgroundBlur(true)
	PlayerPanel:ShowCloseButton(false)
	PlayerPanel:AddCloseButton()
	PlayerPanel:MakePopup()
	
		
	local PlayerSheet = vgui.Create( "DPropertySheet" )
	PlayerSheet:SetParent( PlayerPanel )
	PlayerSheet:SetPos( 150, 40 )
	PlayerSheet:SetSize( PlayerPanel:GetWide() - 170, PlayerPanel:GetTall() - 80 )

	SetJobList = vgui.Create( "DPanelList" )
	SetJobList:SetSpacing( 1 )
	SetJobList:SetPadding( 1 )
	SetJobList:EnableVerticalScrollbar( true ) 
	SetJobList:EnableHorizontal( false )	
	
	GMRP.AddJobCat( "Government", "government" )
	GMRP.AddJobCat( "Public services", "publicservices" )	
	GMRP.AddJobCat( "Commercial", "commercial" )
	GMRP.AddJobCat( "Other", "other" )
	
	PlayerSheet:AddSheet( "Jobs", SetJobList, "gui/silkicons/user", false, false, "Buy permits here" )	
	
 	local ModelList = vgui.Create( "DPanelList" )
	ModelList:SetSpacing( 1 )
	ModelList:SetPadding( 1 )
	ModelList:EnableVerticalScrollbar( true ) 
	ModelList:EnableHorizontal( true )

	for k, v in pairs( citizenmodels ) do
	
		local modelEnt = ents.Create( "prop_physics" )
		modelEnt:SetAngles( Angle( 0, 0, 0 ) )
		modelEnt:SetPos( Vector( 0, 0, 0 ) )
		modelEnt:SetModel( v )
		modelEnt:Spawn()
		modelEnt:Activate()
		modelEnt:PhysicsInit( SOLID_VPHYSICS )   			
	
		local ModelIcon = vgui.Create( "DModelPanel" )
		ModelIcon:SetModel( v )
		ModelIcon:SetSize( 150, 150 )
		local obbCent = modelEnt:OBBCenter()
		local bRad = modelEnt:BoundingRadius()
		ModelIcon:SetCamPos( obbCent + Vector( bRad, bRad, bRad ) )
		ModelIcon:SetLookAt( obbCent )		
		ModelIcon.DoClick = function()
		
			surface.PlaySound( "buttons/lightswitch2.wav" )		
			RunConsoleCommand( "gm_rp_setmodel", v )
			
		end
		
		ModelIcon:InvalidateLayout( true ) 
		modelEnt:Remove()		
		
		ModelList:AddItem( ModelIcon )
		
	end
	
	PlayerSheet:AddSheet( "Player model", ModelList, "gui/silkicons/user", false, false, "Select your player model here." )
		
	local InventoryList = vgui.Create( "DPanelList" )
	InventoryList:SetSpacing( 1 )
	InventoryList:SetPadding( 1 )
	InventoryList:EnableVerticalScrollbar( true ) 
	InventoryList:EnableHorizontal( true )		


	for itemname, item in pairs( GMRP.Items ) do
		
		for k, v in pairs( MyInventory ) do
		
			if ( k == itemname ) then
		
				local invmodelEnt = ents.Create( "prop_physics" )
				invmodelEnt:SetAngles( Angle(0, 0, 0) )
				invmodelEnt:SetPos( Vector( 0, 0, 0 ) )
				invmodelEnt:SetModel( item.Model )
				invmodelEnt:Spawn()
				invmodelEnt:Activate()
				invmodelEnt:PhysicsInit( SOLID_VPHYSICS )   			
			
				local InventoryIcon = vgui.Create( "DModelPanel" )
				InventoryIcon:SetModel( item.Model )
				InventoryIcon:SetSize( 50, 50 )
				local obbCent = invmodelEnt:OBBCenter()
				local bRad = invmodelEnt:BoundingRadius()
				InventoryIcon:SetCamPos( obbCent + Vector( bRad, bRad, bRad ) )
				InventoryIcon:SetLookAt( obbCent )		
				InventoryIcon:SetToolTip( "Name: "..item.Name.."\nDescription: "..item.Info.."\nQuantity: "..v )	
				InventoryIcon.DoClick = function()
				
					local InventoryOptions = DermaMenu()
					
					InventoryOptions:AddOption( "Drop", function() 
					
						if GMRP.UseLimit( LocalPlayer() ) then
										
							surface.PlaySound( "buttons/lightswitch2.wav" )		
							
							if MyInventory[k] - 1 == 0 then InventoryIcon:Remove() end
							
							InventoryIcon:SetToolTip( "Name: "..item.Name.."\nPrice: "..CUR..item.Price.."\nDescription: "..item.Info.."\nQuantity: "..MyInventory[k] - 1 )	

							RunConsoleCommand( "gm_rp_inventory_drop", k )									
							
						end	
						
					end )
					
					InventoryOptions:AddOption( "Use", function() 
					
						if GMRP.UseLimit( LocalPlayer() ) then
						
							if item.Remove then
							
								surface.PlaySound( "buttons/lightswitch2.wav" )
							
								if MyInventory[k] - 1 == 0 then InventoryIcon:Remove() end
								
								InventoryIcon:SetToolTip( "Name: "..item.Name.."\nPrice: "..CUR..item.Price.."\nDescription: "..item.Info.."\nQuantity: "..MyInventory[k] - 1 )	

								RunConsoleCommand( "gm_rp_inventory_use", k )				

							else
							
								LocalPlayer():RedNotify( "You can not use this item from your inventory" )
								
							end	
							
						end	
						
					end )
					
					InventoryOptions:Open()
					
				end
				
				InventoryIcon:InvalidateLayout( true ) 
				invmodelEnt:Remove()						
				
				InventoryList:AddItem( InventoryIcon )
				
			end	
			
		end
		
	end		
	

	PlayerSheet:AddSheet( "Inventory", InventoryList, "gui/silkicons/box", false, false, "Your items are stored here." )
	
	
	local PlayerCompany = vgui.Create( "DPanelList" )
	PlayerCompany:SetSpacing( 2 )
	PlayerCompany:SetPadding( 5 )
	PlayerCompany:EnableVerticalScrollbar( true ) 
	PlayerCompany:EnableHorizontal( false )			
	
	if ( companyData[1] == nil ) then
	
		local clabel1 = vgui.Create( "DLabel" )	
		clabel1:SetPos(90, 40)
		clabel1:SetText( "You're not in any companies, you can" )
		clabel1:SetFont( "GModRPFontOne" )
		PlayerCompany:AddItem( clabel1 )
		
		local CreateNCom = vgui.Create( "DButton" ) 
		CreateNCom:SetTall( 20 )
		CreateNCom:SetText( "create your own company" )
		CreateNCom.DoClick = function()
		
			Derma_StringRequest( "Create new company.", "Enter the name of your company.", "MingeBagCompany", function( text ) RunConsoleCommand( "gm_rp_company_create", text ) end )
			PlayerPanel:Close()
			
		end	
		
		PlayerCompany:AddItem( CreateNCom )	
		
		local clabel2 = vgui.Create( "DLabel" )	
		clabel2:SetPos(90, 40)
		clabel2:SetText( "or get invited to a company by someone." )
		clabel2:SetFont( "GModRPFontOne" )
		PlayerCompany:AddItem( clabel2 )	
		
	
	else
		
	
		local CompanyNameL = vgui.Create( "DLabel" )	
		CompanyNameL:SetPos(90, 40)
		CompanyNameL:SetText( "Name: "..companyData[1] )
		CompanyNameL:SetFont( "GModRPFontOne" )
		PlayerCompany:AddItem( CompanyNameL )			
	
		local CompanyInfoL = vgui.Create( "DLabel" )	
		CompanyInfoL:SetPos(90, 40)
		CompanyInfoL:SetText( "Information: "..companyData[2] )
		CompanyInfoL:SetFont( "GModRPFontOne" )
		PlayerCompany:AddItem( CompanyInfoL )		

		local CompanyAgendaL = vgui.Create( "DLabel" )	
		CompanyAgendaL:SetPos(90, 40)
		CompanyAgendaL:SetText( "Agenda: "..companyData[3] )
		CompanyAgendaL:SetFont( "GModRPFontOne" )
		PlayerCompany:AddItem( CompanyAgendaL )			

		local LeaveTheCompany = vgui.Create( "DButton" ) 
		LeaveTheCompany:SetTall( 20 )
		LeaveTheCompany:SetText( "leave the company" )
		LeaveTheCompany.DoClick = function()
		
			RunConsoleCommand( "gm_rp_company_leave" )
			PlayerPanel:Close()
			
		end	
		
		PlayerCompany:AddItem( LeaveTheCompany )			
	
		if ( Entity( companyData[4][1] ) == LocalPlayer() ) then
		
			local AddMember = vgui.Create( "DButton" ) 
			AddMember:SetTall( 20 )
			AddMember:SetText( "add a member" )
			AddMember.DoClick = function()
			
				Derma_StringRequest( "Add a new member.", "Enter the new member's name.", "MingeBag", function( text ) RunConsoleCommand( "gm_rp_company_addmember", text ) end )
				PlayerPanel:Close()
				
			end	
			
			PlayerCompany:AddItem( AddMember )			
			
		end
	
		local CompanyMemberList = vgui.Create("DListView")
		CompanyMemberList:SetTall( 300 )
		CompanyMemberList:SetMultiSelect(false)
		CompanyMemberList:AddColumn( "Members" )
		 
		for k, v in pairs( companyData[4] ) do
		
			CompanyMemberList:AddLine( Entity( v ):Nick() )
			
		end
		
		PlayerCompany:AddItem( CompanyMemberList )	
		
	end
	
	PlayerSheet:AddSheet( "Companies", PlayerCompany, "gui/silkicons/user", false, false, "Player commands." )	
	
	
	
	PlayerCommandList = vgui.Create( "DPanelList" )
	PlayerCommandList:SetSpacing( 2 )
	PlayerCommandList:SetPadding( 5 )
	PlayerCommandList:EnableVerticalScrollbar( true ) 
	PlayerCommandList:EnableHorizontal( false )			
	
	
	AddPlayerCommandButton( "Toggle sit", "gm_rp_sit" )	
	
	AddPlayerCommandButton( "Toggle sleep", "gm_rp_sleep" )
	
	AddPlayerCommandButton( "Drop weapon", "gm_rp_dropweapon" )	
	
	PlayerSheet:AddSheet( "Player commands", PlayerCommandList, "gui/silkicons/user", false, false, "Player commands." )
	
end	

GMRP.command( "playermenu", GMRP.PlayerPanel )